<template>
  <div>
    
    <el-row class="lock-btn">
      <el-button-group>
        <el-button :disabled="userState" size="medium" type="primary" @click="addBtn" round>添加</el-button>
        <!-- <el-button v-if="roleIndex == false" :disabled="userState" size="medium" type="danger" round @click="batchDelete">删除</el-button> -->
      </el-button-group>
    </el-row>
    
    <el-table style="box-shadow: rgb(205 205 205) 1px 1px 8px;" :header-cell-style="{ background: '#ecf3fd',color:'#000000' }" highlight-current-row  ref="copartnerTable" v-loading="loading" :data="copartners" :stripe="true" :row-key="'id'" :max-height="clientHeight" :height="clientHeight" @selection-change="handleTableChange">
 
      <el-table-column prop="name" label="姓名" width="240" align="center"></el-table-column>
      <el-table-column label="类型" width="100" align="center">
        <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 0" size="medium" type="success">个人</el-tag>
            <el-tag v-else-if="scope.row.type === 1" size="medium" type="danger">企业</el-tag>
          <el-tag v-else size="medium">未知</el-tag>
        </template>
      </el-table-column>
 
      <el-table-column prop="openId" label="openId(一步商户)" width="160" align="center"></el-table-column> 
      <el-table-column prop="phone" label="手机" width="160" align="center"></el-table-column> 
      <el-table-column prop="bankAccount" label="银行账号" width="160" align="center"></el-table-column>
      <el-table-column prop="bankName" label="开户银行" width="180" align="center"></el-table-column>
      <el-table-column prop="merchantNo" label="商户号" width="120" align="center"></el-table-column>
      <!-- <el-table-column prop="merchantKey" label="商户key" width="220" align="center"></el-table-column> -->
      <el-table-column prop="directTransfer" label="直接付款" width="120" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.directTransfer == '0'" size="medium" type="danger">否</el-tag>
          <el-tag v-else-if="scope.row.directTransfer == '1'" size="medium" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="feeRate" label="服务费率" width="100" align="center"></el-table-column>
      <el-table-column prop="bankAddress" label="联系地址" width="180"  align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" width="120" align="center">
        <template slot-scope="scope">
          <el-button :disabled="userState" class="operateBtn" size="mini" type="primary" @click="editBtn(scope.$index, scope.row)" title="修改">
            <i class="el-icon-edit-outline"></i>
          </el-button>
          <el-button v-if="roleIndex == false" :disabled="userState"  class="operateBtn" size="mini" type="danger" @click="delPartner(scope.$index, scope.row)" title="删除">
            <i class="el-icon-delete"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-row>
      <el-col :span="24" class="page-tool">
        <el-pagination layout="total,sizes,prev, pager,next,jumper" :page-size="pageSize" :total="total" :page-sizes="[10, 20, 30, 40, 100]" @size-change="handlePageChange" @current-change="handleNoChange"></el-pagination>
      </el-col>
    </el-row>

    <el-dialog :title="dialogTitle" :visible.sync="addDialog" :width="'422px'">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="姓名" :label-width="labelWidth" prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" :label-width="labelWidth" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" @change="handleChangeType(form.type)">
            <el-option key="0" label="个人" :value="0"></el-option>
            <el-option key="1" label="企业" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员" :label-width="labelWidth" v-if="roleIndex == false" prop="managerId">
          <el-select v-model="form.managerId" placeholder="请选择管理员">
            <el-option v-for="manager in managers" v-bind:key="manager.id" :label="manager.loginName" :value="manager.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="openId" :label-width="labelWidth" prop="openId">
          <el-input v-model="form.openId" auto-complete="off"></el-input>
        </el-form-item>
  
        <el-form-item label="手机" :label-width="labelWidth" prop="phone">
          <el-input v-model="form.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系地址" :label-width="labelWidth" prop="bankAddress">
          <el-input v-model="form.bankAddress" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="银行账号" :label-width="labelWidth" prop="bankAccount">
          <el-input v-model="form.bankAccount" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="开户银行" :label-width="labelWidth" prop="bankName">
          <el-input v-model="form.bankName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商户号" :label-width="labelWidth" v-if="companyIndex" prop="merchantNo">
          <el-input v-model="form.merchantNo" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商户key" :label-width="labelWidth" v-if="companyIndex" prop="merchantKey">
          <el-input v-model="form.merchantKey" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="服务费率(%)" :label-width="labelWidth" prop="feeRate">
          <el-input v-model="form.feeRate" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="直接付款" prop="directTransfer" :label-width="labelWidth" v-if="companyIndex">
          <el-switch v-model="directTransfer"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="savePartner">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  loadPartnerApi,
  addPartnerApi,
  updatePartnerApi,
  delPartnerApi,
  batchDelPartnerApi
} from "../../api/partner.js";
import { loadManager2Api } from "../../api/manager";
import { getNickName, getTestUserName, getRoleId } from "../../util/auth";
export default {
  data () {
    return {
      clientHeight:
        Math.max(
          document.documentElement.scrollHeight,
          document.body.scrollHeight
        ) -
        132 -
        5 +
        10 -
        28,
      sexOptions: ["女", "男"],
      labelWidth: "120px",
      loading: false,
      dialogTitle: "",
      pageNo: 1,
      pageSize: 20,
      total: 0,
      copartners: [],
      addDialog: false,
      form: {
        name: '',
        type: '',
        openId: '',
        openId2: '',
        phone: '',
        bankAddress: '',
        bankAccount: '',
        bankName: '',
        merchantNo: null,
        merchantKey: null,
        feeRate: 3.0,
        directTransfer: 0,
      },
      multipleSelection: [],
      directTransfer: false,
      personalIndex: false,
      companyIndex: false,

      managers: [],
      roleIndex: true,

      rules: {
        name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
        type: [
            { required: true, message: '请选择类型', trigger: 'change' }
          ],
        managerId: [
            { required: true, message: '请选择管理员', trigger: 'change' }
          ],
        openId: [
            { required: true, message: '请输入openId', trigger: 'blur' }
          ],
        openId2: [
            { required: true, message: '请输入openId2', trigger: 'blur' }
          ],
        phone: [
            { required: true, message: '请输入联系方式', trigger: 'blur' }
          ],
        bankAddress: [
            { required: true, message: '请输入联系地址', trigger: 'blur' }
          ],
        bankAccount: [
            { required: true, message: '请输入银行账号', trigger: 'blur' }
          ],
        bankName: [
            { required: true, message: '请输入开户银行', trigger: 'blur' }
          ],
        merchantNo: [
            { required: true, message: '请输入商户号', trigger: 'blur' }
          ],
        merchantKey: [
            { required: true, message: '请输入商户key', trigger: 'blur' }
          ],
        feeRate: [
            { required: true, message: '请输入服务费率(%)', trigger: 'blur' }
          ],
      },

    };
  },
  methods: {
    loadPartners (pageNo, pageSize) {
      let _this = this;
      _this.loading = true;
      loadPartnerApi({ pageNo: pageNo, pageSize: pageSize })
        .then(resp => {
          _this.copartners = resp.data.dataList;
          _this.total = resp.data.totalNum;
          _this.loading = false;
        })
        .catch(e => {
          this.loaded = false;
          this.loading = false;
          console.log(e);
        });
    },

    loadManagers (pageNo, pageSize) {
      let _this = this;
      if(!_this.roleIndex){
        _this.loading = true;
        loadManager2Api({ pageNo: pageNo, pageSize: pageSize })
        .then(resp => {
          _this.managers = resp.data.dataList;
          _this.total = resp.data.totalNum;
          _this.loading = false;
        })
        .catch(e => {
          this.loaded = false;
          this.loading = false;
          console.log(e);
        });
      }
    },

    savePartner () {
      let _this = this;
      _this.$refs.form.validate((valid) => {
        if (valid) {
          if(_this.directTransfer){
          _this.form.directTransfer = 1;
          }else{
            _this.form.directTransfer = 0;
          }
          if (_this.dialogTitle === "添加合伙人") {

            _this.form.feeRate = _this.form.feeRate / 100.0;

            addPartnerApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadPartners(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "合伙人添加成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "合伙人添加失败",
                  type: "error"
                });
              }
            });
          } else {

            _this.form.feeRate = _this.form.feeRate / 100.0;

            updatePartnerApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadPartners(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "合伙人修改成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "合伙人修改失败",
                  type: "error"
                });
              }
            });
          }
        }
      });
      
    },
    addBtn () {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.form != undefined){
          _this.$refs.form.resetFields();
        }
      })
      _this.loadManagers()
      _this.dialogTitle = "添加合伙人";
      _this.form = {
        name: '',
        type: 0,
        openId: '',
        openId2: '',
        phone: '',
        bankAddress: '',
        bankAccount: '',
        bankName: '',
        merchantNo: null,
        merchantKey: null,
        feeRate: 3.0,
        directTransfer: 0,
      };
      _this.personalIndex = true;
      _this.companyIndex = false;
      _this.addDialog = true;
    },
    editBtn (index, row) {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.form != undefined){
          _this.$refs.form.resetFields();
        }
      })
      _this.loadManagers()
      _this.dialogTitle = "修改合伙人";
      _this.form = {
        id:row.id,
        name: row.name,
        type: row.type,
        managerId: row.managerId,
        openId: row.openId,
        openId2: row.openId2,
        phone: row.phone,
        bankAddress: row.bankAddress,
        bankAccount: row.bankAccount,
        bankName: row.bankName,
        merchantNo: row.merchantNo,
        merchantKey: row.merchantKey,
        feeRate: row.feeRate*100,
        directTransfer: row.directTransfer,
      };
      if(_this.form.directTransfer == 0){
        _this.directTransfer = false
      }else{
        _this.directTransfer = true
      }
      if(_this.form.type===0){
        _this.personalIndex = true;
        _this.companyIndex = false;
      }
      if(_this.form.type===1){
        _this.personalIndex = false;
        _this.companyIndex = true;
      }
      _this.addDialog = true;
    },
    handlePageChange (pageSize) {
      this.pageSize = pageSize;
      this.loadPartners(this.pageNo, this.pageSize);
    },
    handleNoChange (pageNo) {
      this.pageNo = pageNo;
      this.loadPartners(this.pageNo, this.pageSize);
    },
    delPartner (index, row) {
      let _this = this;
      this.$confirm('确认删除合伙人“' + row.name + '”', '提示', {
        type: 'warning'
      })
        .then(_ => {
          delPartnerApi(row)
            .then(function (resp) {
              if (resp.errmsg === "ok") {
                _this.loadPartners(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "合伙人删除成功",
                  type: "success"
                });
              }
            })
        })
    },
    handleTableChange (val) {
      console.log("handleTableChange", val);
      this.multipleSelection = val;
    },
    batchDelete () {
      if (this.multipleSelection.length == 0) {
        this.$message({
          message: "请先选择合伙人",
          type: "warning"
        });
        return;
      }
      let _this = this;
      this.$confirm('确认批量删除合伙人', '提示', {
        type: 'warning'
      })
      .then(_ =>{
        batchDelPartnerApi(this.multipleSelection).then(function (resp) {
          if (resp.errmsg === "ok") {
            _this.loadPartners(_this.pageNo, _this.pageSize);
            _this.$refs.copartnerTable.clearSelection();
            _this.$notify({
              title: "成功",
              message: "合伙人删除成功",
              type: "success"
            });
          }
        });
      })
    },

    handleChangeType (type) {
      if(type === 0){
        this.personalIndex = true;
        this.companyIndex = false;
        this.form.merchantKey = '';
        this.form.merchantNo = '';
        this.directTransfer = false;
      }
      if(type === 1){
        this.personalIndex = false;
        this.companyIndex = true;
        this.form.openId = '';
      }
    },
    
  },

  computed: {
    userState () {
      return getNickName() === getTestUserName();
    }
  },
  mounted: function () {
    this.$nextTick(function () {
      this.loadPartners(this.pageNo, this.pageSize);
      this.roleIndex = getRoleId()==1?false:true;
    });
  }
};
</script>
<style>
.page-tool {
  background:  rgb(237, 231, 231);
}

.page-tool div {
  text-align: right;
}

.lock-btn {
  margin-bottom: 5px;
  margin-top: -10px;
}
.operateBtn {
  font-size: 18px;
  width: 32px;
  height: 32px;
  padding: 0;
}
 
</style>
